package org.ftc.web;

import org.ftc.domain.OpenTime;
import org.ftc.enums.Constant;
import org.ftc.service.FtcLogService;
import org.ftc.service.OpenTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OpenTimeController {
	
	@Autowired
	OpenTimeService openTimeService;
	@Autowired
	FtcLogService logService;
	
	@RequestMapping(value = "/openTime.htm")
	public String index(ModelMap model) {
		OpenTime openTime = openTimeService.get(Constant.MONTH.name());
		if(openTime==null){
			openTime = new OpenTime();
			openTime.setName(Constant.MONTH.name());
			openTime.setMonth("6");
			openTimeService.save(openTime);
		}
		model.addAttribute("month", openTime.getMonth());
		return "openTime/operation";
	}
	
	@RequestMapping(value = "/openTime/{month}.json", method = RequestMethod.GET)
	public @ResponseBody boolean active(@PathVariable String month) {
		OpenTime openTime = openTimeService.get("MONTH");
		openTime.setMonth(month);
		return openTimeService.update(openTime);
	}

}
